package com.edu.internship.util;

import com.edu.internship.busi.entity.UserToken;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

//JWT工具类 JWT可以自校验
public class JwtUtils {

    //密钥
    private static final String SALT = "kxoif%$*hdas$@_dlsd";
    //过期时间
    private static final Integer EXPIRE_TIME = 1000 * 60 * 30;
    //生成token
    public static String getToken(UserToken userToken){
        String token = Jwts.builder()
                //设置开始时间
                .setIssuedAt(new Date(System.currentTimeMillis()))
                //设置有效期
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE_TIME))
                //设置用户声明
                .claim("id", userToken.getId())
                .claim("name", userToken.getUsername())
                //签名
                .signWith(SignatureAlgorithm.HS256, SALT)
                .compact();

        System.out.println(token);
        return token;
    }
    //验证token
    public static void isExpire(String token){
      Jwts.parser().setSigningKey(SALT).parseClaimsJws(token);
    }
    public static boolean isExpire(HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return false;
        }
        try {
            Jwts.parser().setSigningKey(SALT).parseClaimsJws(token);
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        return false;
    }

    /**
     * 获取token信息
     * @param token
     * @return
     */
    public static String getId(String token){
        Claims body = Jwts.parser().setSigningKey(SALT).parseClaimsJws(token).getBody();
        return (String) body.get("id");
    }
    public String getId(HttpServletRequest request){
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey(SALT).parseClaimsJws(token).getBody();
        return (String) body.get("id");
    }
    public static String getUsername(String token){
        Claims body = Jwts.parser().setSigningKey(SALT).parseClaimsJws(token).getBody();
        return (String) body.get("username");
    }
    public static String getImage(String token){
        Claims body = Jwts.parser().setSigningKey(SALT).parseClaimsJws(token).getBody();
        return (String) body.get("image");
    }

}